package com.example.module_service.device

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_common.view.entity.CarAddParam
import com.example.lib_base.path.RouterPath
import com.example.lib_common.view.entity.CarplateEntity
import com.example.lib_common.view.view.BaseMvvmActivity
import com.example.module_service.R
import com.example.module_service.adapter.ShengfenAdapter
import com.example.module_service.databinding.ActivityCarBinding
import com.example.module_service.viewmodel.ServerViewModel
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route(path = RouterPath.PATH_CAR_PAGE)
class CarActivity : BaseMvvmActivity<ActivityCarBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_car

    private val carViewModel: ServerViewModel by viewModels()
    private lateinit var shengfenAdapter: ShengfenAdapter
    private val detaillist = mutableListOf<CarplateEntity>()
    var index:Int=0
    // 定义省份简称列表
    private val provinceList = listOf(
        "京", "津", "沪", "渝", "冀", "豫", "云", "辽", "黑", "湘",
        "皖", "鲁", "新", "苏", "浙", "赣", "鄂", "桂", "甘", "晋",
        "蒙", "陕", "吉", "闽", "贵", "粤", "青", "藏", "川", "宁",
        "琼", "港", "澳", "台"
    )

    override fun initViews() {

        shengfenAdapter=ShengfenAdapter()
        val editText: Array<EditText> = arrayOf(mDataBinding.et1,mDataBinding.et2,mDataBinding.et3,mDataBinding.et4,mDataBinding.et5,mDataBinding.et6,mDataBinding.et7,mDataBinding.et8)
        //车牌
        var carList= arrayOf("鄂","京","津","冀","晋","蒙","辽","吉","黑","沪","苏","浙","皖","闽","赣","鲁","豫","鄂","湘","粤","桂","琼","渝","川","贵","云","藏","陕","甘","青","宁","新"," "," ","X")
        var carplateEntity=ArrayList<CarplateEntity>()
        for (i in carList){
            index=i.length-1
            carplateEntity.add(CarplateEntity(i))
        }
        mDataBinding.rv.adapter=shengfenAdapter
        mDataBinding.rv.layoutManager= GridLayoutManager(this,7)
        shengfenAdapter.data.clear()
        shengfenAdapter.data.addAll(carplateEntity)
        shengfenAdapter.notifyDataSetChanged()
        mDataBinding.carTime.text = SimpleDateFormat("2026-MM-dd HH:mm:ss").format(System.currentTimeMillis())
        shengfenAdapter.setOnItemClickListener { adapter, view, position ->
            if (carList[position].equals("X")){
                mDataBinding.et1.text.clear()
                mDataBinding.et2.text.clear()
                mDataBinding.et3.text.clear()
                mDataBinding.et4.text.clear()
                mDataBinding.et5.text.clear()
                mDataBinding.et6.text.clear()
                mDataBinding.et7.text.clear()
                mDataBinding.et8.text.clear()
                mDataBinding.carCode.text=""
            }else{
                editText[0].setText(carList[position])
            }

        }
        for (i in editText.indices){
            editText[i].addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

                }

                override fun afterTextChanged(s: Editable?) {
                    if (s?.length==1){
                        if (index<editText.size-1){
                            index++
                            editText[index].requestFocus()
                        }
                        if (index==editText.size-1){
                            mDataBinding.carCode.text=mDataBinding.et1.text.toString()+mDataBinding.et2.text.toString()+"-"+mDataBinding.et3.text.toString()+mDataBinding.et4.text.toString()+mDataBinding.et5.text.toString()+mDataBinding.et6.text.toString()+mDataBinding.et6.text.toString()+mDataBinding.et8.text.toString()
                        }
                    }else if (s?.length==0){
                        if (index>0){
                            editText[index-1].requestFocus()
                        }
                    }
                }
            })
        }
        mDataBinding.btnAddCar.setOnClickListener {
            if (mDataBinding.carCode.text.isEmpty()) {
                ToastUtils.showLong("请输入车牌号")
                return@setOnClickListener
            }else{
                val carAddParam = CarAddParam(
                    id = 0,
                    licensePlate = mDataBinding.carCode.text.toString(),
                    ownerName = "人海",
                    provinceAbbreviation = mDataBinding.et1.text.toString(),
                    userId = 1
                )
                carViewModel.getAddCar(carAddParam)
            }
        }
        carViewModel.addCar.observe(this){
            ToastUtils.showLong("添加成功")
            ARouter.getInstance().build(RouterPath.PATH_CAR_PAY_PAGE)
                .withString("carCode",mDataBinding.carCode.text.toString())
                .navigation()
        }
//        carViewModel.mErrorLiveData.observe(this){
//            ToastUtils.showLong(it)
//        }
    }

    override fun loadData() {
        super.loadData()
        loadLocalProvinceData()
    }

    override fun observeDataChang() {
        super.observeDataChang()
        carViewModel.CarSUccess.observe(this) {
            detaillist.clear()
            detaillist.addAll(it)
            mDataBinding.rv.adapter?.notifyDataSetChanged()
        }
    }

    private fun loadLocalProvinceData() {
        detaillist.clear()
        provinceList.forEach { province ->
            detaillist.add(CarplateEntity(province))
        }
        mDataBinding.rv.adapter?.notifyDataSetChanged()
    }

    override fun getActivityState(state: Bundle) {}

    override fun saveActivityState(outState: Bundle) {}
}

